package com.ckerchina.admin.ckadmin.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ckerchina.admin.ckadmin.cache.MemcachedService;
import com.ckerchina.admin.ckadmin.dao.AppUserDAO;
import com.ckerchina.admin.ckadmin.domain.AppUser;

/**
 * Created by 晓丰 on 2015/12/13.
 */
@Service
public class AppUserService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppUserDAO appUserDAO;

    private String namespace = AppUser.class.getName();

    @Autowired
    private MemcachedService memcachedService;


    @Transactional
    public void persist(AppUser appUser){
    		appUserDAO.persist(appUser);
        memcachedService.setWithNamespace(namespace,String.valueOf(appUser.getId()),appUser);

    }
    
    @Transactional
    public void merge(AppUser appUser){
    		appUserDAO.merge(appUser);
        memcachedService.setWithNamespace(namespace,String.valueOf(appUser.getId()),appUser);
    }

    @Transactional
    public void mergeAppuser(AppUser appUser){
        AppUser olduser = appUserDAO.find(appUser.getId());
        if(olduser != null){
            olduser.setMobile(appUser.getMobile());
            olduser.setName(appUser.getName());
            olduser.setToken(appUser.getToken());
            merge(olduser);
        }
    }
   
    public AppUser find(long userid) {
        return appUserDAO.find(userid);
    }
}
